home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / xstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.6 KB  |  180 lines

  1. /* xstring.c -- Code for needed string functions that might be missing.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  strcasecmp() and strncasecmp()
  20.    have been stolen from the GNU C library.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <sys/types.h>
  28. #include <ctype.h>
  29. #include <stddef.h>
  30.  
  31. #include "xmalloc.h"
  32. #include "xstring.h"
  33.  
  34.  
  35. #ifndef HAVE_STRCASECMP
  36.  
  37. int
  38. strcasecmp(s1, s2)
  39.     const char *s1;
  40.     const char *s2;
  41. {
  42.     unsigned char c1, c2;
  43.  
  44.     if (s1 == s2)
  45.     return 0;
  46.  
  47.     do
  48.     {
  49.     c1 = tolower(*s1++);
  50.     c2 = tolower(*s2++);
  51.  
  52.     if (c1 == 0)
  53.         break;
  54.     }
  55.     while (c1 == c2);
  56.  
  57.     return c1 - c2;
  58. }
  59.  
  60. #endif /* !HAVE_STRCASECMP */
  61.  
  62.  
  63. #ifndef HAVE_STRNCASECMP
  64.  
  65. /*
  66.  * Compare no more than N characters of S1 and S2,
  67.  * ignoring case, returning less than, equal to or
  68.  * greater than zero if S1 is lexicographically less
  69.  * than, equal to or greater than S2.
  70.  */
  71.  
  72. int
  73. strncasecmp(s1, s2, n)
  74.     const char *s1;
  75.     const char *s2;
  76.     size_t n;
  77. {
  78.     unsigned char c1, c2;
  79.     register const unsigned char *p1 = (const unsigned char *) s1;
  80.     register const unsigned char *p2 = (const unsigned char *) s2;
  81.  
  82.     if (p1 == p2 || n == 0)
  83.     return 0;
  84.  
  85.     do
  86.     {
  87.     c1 = tolower (*p1++);
  88.     c2 = tolower (*p2++);
  89.     if (c1 == '\0' || c1 != c2)
  90.         return c1 - c2;
  91.     }
  92.     while (--n > 0);
  93.  
  94.     return c1 - c2;
  95. }
  96.  
  97. #endif /* !HAVE_STRNCASECMP */
  98.  
  99.  
  100. #ifndef HAVE_STRSTR
  101.  
  102. /*
  103.  * Return the first ocurrence of NEEDLE in HAYSTACK.
  104.  */
  105.  
  106. char *
  107. strstr(haystack, needle)
  108.     const char *haystack;
  109.     const char *needle;
  110. {
  111.     register char *needle_end   = strchr(needle, '\0');
  112.     register char *haystack_end = strchr(haystack, '\0');
  113.     register size_t needle_len  = needle_end - needle;
  114.     register size_t needle_last = needle_len - 1;
  115.     register char *begin;
  116.  
  117.     if (needle_len == 0)
  118.     return (char *) haystack;       /* ANSI 4.11.5.7, line 25.  */
  119.  
  120.     if ((size_t) (haystack_end - haystack) < needle_len)
  121.     return NULL;
  122.  
  123.     for (begin = &haystack[needle_last]; begin < haystack_end; ++begin)
  124.     {
  125.     register char *n = &needle[needle_last];
  126.     register char *h = begin;
  127.  
  128.     do
  129.         if (*h != *n)
  130.         goto loop;              /* continue for loop */
  131.     while (--n >= needle && --h >= haystack);
  132.  
  133.     return (char *) h;
  134.  
  135.     loop:;
  136.     }
  137.  
  138.     return NULL;
  139. }
  140.  
  141. #endif /* !HAVE_STRSTR */
  142.  
  143.  
  144. /*
  145.  * A strdup() version that calls xmalloc instead of malloc, never returning
  146.  * a NULL pointer.
  147.  */
  148.  
  149. char *
  150. xstrdup(s)
  151.     const char *s;
  152. {
  153.     size_t len = strlen(s) + 1;
  154.     char *new_s = xmalloc(len);
  155.  
  156.     memcpy(new_s, s, len);
  157.     return new_s;
  158. }
  159.  
  160.  
  161. #ifndef HAVE_MEMMOVE
  162.  
  163. /* A slow but portable memmove function.  For those loosing systems that
  164.    don't have one.  */
  165.  
  166. void *
  167. memmove(dest, src, n)
  168.     void *dest;
  169.     const void *src;
  170.     size_t n;
  171. {
  172.     char *temp = xmalloc(n);
  173.     memcpy(temp, src, n);
  174.     memcpy(dest, temp, n);
  175.     xfree(temp);
  176.     return dest;
  177. }
  178.  
  179. #endif /* !HAVE_MEMMOVE */
  180.